package com.health;

import com.health.model.SystemSettings;
import com.health.tools.DownLoadTools;
import com.health.tools.GetPost;
import com.health.tools.GlobalConts;
import com.health.tools.SPUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 系统设置
 * 
 * @author 田相伟
 *
 */
public class SystemSettingActivity extends NovaActivity {
	public EditText ftp_path;// ftp地址
	public EditText ftp_account;// ftp帐号
	public EditText ftp_passward;// ftp密码
	public EditText db_path;// 数据库地址
	public EditText date_tongbu_path;// 数据同步地址
	public EditText shangchuan_yanzheng;// 上传验证接口
	public EditText shengji_hao;// 产品升级号
	public EditText zidong_shengji;// 自动升级
	public Button save;// 保存
	public Button cancle;// 取消
//	static SharedPreferences MyPreferences = null;
	private static String pathStr = Environment.getExternalStorageDirectory() + GlobalConts.TYPE_DBFILENAME;
	private Button fanhui;
	private Button up;
	private TextView title;
	private Button set_xzq;
	private Button set_update;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_systemsetting);
		findViews();

		setListeners();
		initData();

	}

	public void initData() {
		if (TextUtils.isEmpty(db_path.getText())) {
			db_path.setText(pathStr + "/KioskDB.db");
		}
		SystemSettings sys=SPUtil.getSysSettings(SystemSettingActivity.this);
		if (!TextUtils.isEmpty(sys.getFtpAddress())) {
			ftp_path.setText(sys.getFtpAddress());
		}
		if (!TextUtils.isEmpty(sys.getFtpAccount())) {
			ftp_account.setText(sys.getFtpAccount());
		}
		if (!TextUtils.isEmpty(sys.getFtpPassword())) {
			ftp_passward.setText(sys.getFtpPassword());
		}
		if (!TextUtils.isEmpty(sys.getDb_path())) {
			db_path.setText(sys.getDb_path());
		}
		if (!TextUtils.isEmpty(sys.getDate_tongbu_path())) {
			date_tongbu_path.setText(sys.getDate_tongbu_path());
		}
		if (!TextUtils.isEmpty(sys.getShangchuan_yanzheng())) {
			shangchuan_yanzheng.setText(sys.getShangchuan_yanzheng());
		}
		if (!TextUtils.isEmpty(sys.getShengji_hao())) {
			shengji_hao.setText(sys.getShengji_hao());
		}
		if (!TextUtils.isEmpty(sys.getZidong_shengji())) {
			zidong_shengji.setText(sys.getZidong_shengji());
		}
	}

	/**
	 * 显示同步数据
	 * 
	 * @param context
	 * @author 王婷婷
	 * @date 2017年3月14日 下午2:51:30
	 */
	private void showSynDialog(final Context context) {
		// 获得SharedPreferences对象
		// 获得SharedPreferences.Editor对象
		LayoutInflater inflater = LayoutInflater.from(this);
		final View textEntryView = inflater.inflate(R.layout.syn_dialog, null);
		final EditText ed_xzbh = (EditText) textEntryView.findViewById(R.id.ed_xzbh);
 		ed_xzbh.setText(SPUtil.getstr_xzq(SystemSettingActivity.this));//MyPreferences.getString("str_xzq", "370188")
		final EditText ed_pwd = (EditText) textEntryView.findViewById(R.id.ed_pwd);
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setCancelable(false);
		builder.setView(textEntryView);
		builder.setPositiveButton("同步", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String str_xzbh = ed_xzbh.getText().toString();
				if (TextUtils.isEmpty(str_xzbh) || str_xzbh.length() != 6) {
					showTextDialog("请输入正确的行政编号");
					return;
				}
				String str_pwd = ed_pwd.getText().toString();
				if (str_pwd.equals("nova8888")) {
					String fileURl = GlobalConts.getUrlPost() + GetPost.URL_DBSYNC + "?orgDistrictId=" + str_xzbh;
					Log.w("eeeeeeeeeeeeee", fileURl);
					if (!TextUtils.isEmpty(fileURl)) {
//						editor.putString("str_xzq", str_xzbh);
//						editor.commit();
						SPUtil.setstr_xzq(context, str_xzbh);
						DownLoadTools downLoad = new DownLoadTools(context);
						downLoad.downloadFile(fileURl, NovaApplication.dbPath);
					}
				} else {
					showTextDialog("密码有误，请重新输入");
				}

			}
		});
		builder.setNegativeButton("取消", null);
		builder.show();
	}

	@Override
	public void findViews() {
		ftp_path = (EditText) findViewById(R.id.ftp_path);
		ftp_account = (EditText) findViewById(R.id.ftp_acount);
		ftp_passward = (EditText) findViewById(R.id.ftp_passward);
		db_path = (EditText) findViewById(R.id.db_path);
		date_tongbu_path = (EditText) findViewById(R.id.date_tongbu_path);
		shangchuan_yanzheng = (EditText) findViewById(R.id.shangchuan_yanzheng);
		save = (Button) findViewById(R.id.save);
		cancle = (Button) findViewById(R.id.cancle);
		shengji_hao = (EditText) findViewById(R.id.shengji_hao);
		int vercode=GlobalConts.getVerCode(getApplicationContext());
		if(vercode==-1){
			shengji_hao.setText("1");
		}else{
			shengji_hao.setText(String.valueOf(vercode));
		}
		
		
		zidong_shengji = (EditText) findViewById(R.id.zidong_shengji);
		fanhui = (Button) findViewById(R.id.btn_back);
		up = (Button) findViewById(R.id.btn_up);
		up.setVisibility(View.GONE);
		title = (TextView) findViewById(R.id.title);
		title.setText("系统设置");
		set_xzq = (Button) findViewById(R.id.set_xzq);
		set_update=(Button)findViewById(R.id.set_update);
	}

	@Override
	public void setListeners() {
		save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				Editor editor = MyPreferences.edit();
//				editor.putString("ftpAddress", ftp_path.getText().toString());
//				editor.putString("ftpAccount", ftp_account.getText().toString());
//				editor.putString("ftpPassword", ftp_passward.getText().toString());
//				editor.putString("db_path", db_path.getText().toString());
//				editor.putString("date_tongbu_path", date_tongbu_path.getText().toString());
//				editor.putString("shangchuan_yanzheng", shangchuan_yanzheng.getText().toString());
//				editor.putString("shengji_hao", shengji_hao.getText().toString());
//				editor.putString("zidong_shengji", zidong_shengji.getText().toString());
//				editor.commit();
				SystemSettings sys=new SystemSettings();
				sys.setFtpAddress(ftp_path.getText().toString());
				sys.setFtpAccount(ftp_account.getText().toString());
				sys.setFtpPassword(ftp_passward.getText().toString());
				sys.setDb_path(db_path.getText().toString());
				sys.setDate_tongbu_path(date_tongbu_path.getText().toString());
				sys.setShangchuan_yanzheng(shangchuan_yanzheng.getText().toString());
				sys.setShengji_hao(shengji_hao.getText().toString());
				sys.setZidong_shengji(zidong_shengji.getText().toString());
				SPUtil.setSysSettings(SystemSettingActivity.this, sys);
				
				GlobalConts.showToast(SystemSettingActivity.this, "保存成功");
				finish();
			}
		});
		cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		// 设置行政区
		set_xzq.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showSynDialog(SystemSettingActivity.this);
			}
		});
		fanhui.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
		set_update.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					if (!GlobalConts.isNetworkAvailable(SystemSettingActivity.this)) {
						showTextDialog("请检查网络");
						return;
					}
					UpdateVersion up = new UpdateVersion(SystemSettingActivity.this);
					up.setSetDialogFlag(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}
}
